import Vue from 'vue'
import Router from 'vue-router'
import Store from '@/store'
import Home from '@/views/pc/home.vue'
import Login from '@/views/pc/login.vue'
import pageNotFound from '@/components/base/404.vue'

// 个人中心
import MessageManage from '@/views/pc/person/message-manage.vue'
import UserInfo from '@/views/pc/person/user-info.vue'
// 管理面板
import UserManage from '@/views/pc/admin/user-manage.vue'

const router = new Router({
  mode: 'history',
  base: `/`,
  routes: [
    {
      path: '/',
      name: '首页',
      component: Home,
      // redirect: '/index',
      children: [
        {
          path: '/person/message',
          name: '消息列表',
          component: MessageManage
        }, {
          path: '/person/info',
          name: '个人资料',
          component: UserInfo
        }, {
          path: '/admin/usermanage',
          name: '员工管理',
          component: UserManage
        },
      ]
    },
    {
      path: '/login',
      name: '登陆',
      component: Login
    },
    {
      path: '*',
      name: '404',
      component: pageNotFound
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.matched && to.matched.length >= 2) {
    // 跳转二级路由，才涉及标签操作
    if(Store.state.cacheTab.indexOf(to.matched[to.matched.length - 1].components.default.name) === -1){
      // 新增缓存页面
      let currentName = to.matched[to.matched.length - 1].components.default.name
      if (Store.state.cacheTab.indexOf(currentName) === -1) {
        Store.state.cacheTab.push(currentName);
      }
      // 新增标签
      Store.state.tabList.push({
        name: to.name,
        url: to.path,
      });
      // 设置当前页面
    }
    Store.state.currentTab = to.path // 设置当前标签
  }
  next();
})

Vue.use(Router)

export default router


